'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 14_runnable_parallel_with_function.py
* @Time: 2025/10/23
* @All Rights Reserve By Brtc
'''
from operator import itemgetter

import dotenv
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableParallel
from langchain_openai import ChatOpenAI

dotenv.load_dotenv()

def retriever_from_database(query:str)->str:
    print("正在检索数据库，检索的问题是：", query)
    return "我叫吴彦祖， 今年18岁， 身价1000个亿， 钱花不完 好烦！！"
# 构建提示词
prompt = ChatPromptTemplate.from_template("""
请根据用户的问题回答, 可以参考对应的上下文进行生成
<context>
{context}
用户的提问是:{query}
""")
llm = ChatOpenAI(model="gpt-4o-mini")
parser = StrOutputParser()
chain = RunnableParallel({
    "context":lambda x : retriever_from_database(x["query"]),
    "query":itemgetter("query")
})|prompt|llm|parser

content = chain.invoke({"query":"你好请问我叫什么？请根据我的现状给我一些人生建议"})
print(content)